﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GSI.Core
{
	class DataHandler
	{
		public static List<GPoint> ReadDataFromFile()
		{
			//选择数据文件窗口
			OpenFileDialog fileDialog = new OpenFileDialog();
			fileDialog.Title = "采样点数据文件";
			fileDialog.Filter = "数据文件（.txt）|*.txt";
			//要返回的数据列表
			List<GPoint> list = new List<GPoint>();
			//获取数据文件路径
			if (fileDialog.ShowDialog() == DialogResult.OK)
			{
				var filePath = fileDialog.FileName;
				//读取文件流
				StreamReader sr = new StreamReader(filePath);
				string line;

				//一次读取一行，每行数据用“，”分割开
				while ((line = sr.ReadLine()) != null)
				{
					string[] lineData = line.Split(new char[] { ',', '\t' });
					if (lineData.Length < 3) break;
					double x = Double.Parse(lineData[0]);
					double y = Double.Parse(lineData[1]);
					double value = Double.Parse(lineData[2]);
					list.Add(new GPoint {
						X = x,
						Y = y,
						Value = value
					});
				}
				// 关闭流
				sr.Close();
			}
			return list;
		}

	}
}
